<template>
  <div class="rich-text-editor-container">
    <!-- 标题区 -->
    <div class="editor-header">
      <h2 class="editor-title">把今天的烦恼写下来</h2>
      <p class="editor-subtitle">米奇妙妙屋</p>
    </div>

    <!-- 富文本编辑器 -->
    <div class="editor-wrapper" :class="{ 'disabled-state': !isEditable }">
      <editor-item
          v-model="content"
          :isClear="isClear"
          :state="isEditable"
          @change="handleContentChange"
      ></editor-item>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons">
      <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          @click="clearEditor"
          class="action-btn"
      >
        清空内容
      </el-button>

      <el-button
          :type="isEditable ? 'warning' : 'success'"
          plain
          :icon="isEditable ? 'el-icon-close' : 'el-icon-edit'"
          @click="toggleEditState"
          class="action-btn"
      >
        {{ isEditable ? '禁用编辑' : '启用编辑' }}
      </el-button>

      <el-button
          type="primary"
          icon="el-icon-check"
          @click="submitContent"
          class="action-btn submit-btn"
      >
        提交内容
      </el-button>
    </div>

    <!-- 预览区域 -->
    <div class="preview-section" v-if="content">
      <h3 class="preview-title">内容预览</h3>
      <div class="preview-content" v-html="content"></div>
    </div>
  </div>
</template>

<script>
import EditorItem from '@/utils/wangeditor.vue'

export default {
  components: {
    EditorItem
  },
  data() {
    return {
      content: '',
      isClear: false,
      isEditable: true
    }
  },
  methods: {
    clearEditor() {
      this.$confirm('确定要清空编辑器内容吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.isClear = true
        setTimeout(() => {
          this.isClear = false
          this.$message.success('内容已清空')
        }, 100)
      }).catch(() => {})
    },

    toggleEditState() {
      this.isEditable = !this.isEditable
      this.$message({
        message: this.isEditable ? '编辑器已启用' : '编辑器已禁用',
        type: this.isEditable ? 'success' : 'warning'
      })
    },

    handleContentChange(newContent) {
      console.log('内容变更:', newContent)
    },

    submitContent() {
      if (!this.content.trim()) {
        this.$message.error('内容不能为空')
        return
      }

      this.$notify({
        title: '提交成功',
        message: '您的内容已提交',
        type: 'success',
        duration: 2000
      })

      // 这里可以添加实际提交逻辑
      console.log(  this.content)
    //   在这个地方去把这个数据保存到这个数据库当中进去
    }
  }
}
</script>

<style scoped>
.rich-text-editor-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 32px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  font-family: 'Helvetica Neue', Arial, sans-serif;
}

.editor-header {
  margin-bottom: 28px;
  padding-bottom: 16px;
  border-bottom: 1px solid #eaeef5;
}

.editor-title {
  color: #1f2d3d;
  font-size: 26px;
  font-weight: 600;
  margin-bottom: 8px;
  letter-spacing: 0.5px;
}

.editor-subtitle {
  color: #8492a6;
  font-size: 15px;
  margin: 0;
  font-weight: 400;
}

.editor-wrapper {
  border: 1px solid #e0e6ed;
  border-radius: 6px;
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

.editor-wrapper:hover {
  border-color: #c0ccda;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.editor-wrapper.disabled-state {
  opacity: 0.65;
  background-color: #f8fafc;
  pointer-events: none;
}

.action-buttons {
  margin-top: 28px;
  display: flex;
  justify-content: center;
  gap: 18px;
  flex-wrap: wrap;
}

.action-btn {
  padding: 11px 24px;
  font-size: 15px;
  border-radius: 6px;
  transition: all 0.3s ease;
  font-weight: 500;
  letter-spacing: 0.3px;
  min-width: 120px;
}

.action-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}

.action-btn:active {
  transform: translateY(0);
}

.submit-btn {
  background: linear-gradient(135deg, #409EFF, #337ecc);
  border: none;
  color: white;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}

.submit-btn:hover {
  background: linear-gradient(135deg, #337ecc, #266499);
  box-shadow: 0 4px 16px rgba(51, 126, 204, 0.3);
}

.preview-section {
  margin-top: 42px;
  padding: 24px;
  background-color: #f8fafc;
  border-radius: 8px;
  border-left: 4px solid #409EFF;
  transition: all 0.3s ease;
}

.preview-section:hover {
  box-shadow: 0 2px 12px rgba(64, 158, 255, 0.08);
}

.preview-title {
  color: #409EFF;
  font-size: 19px;
  font-weight: 500;
  margin-top: 0;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
}

.preview-title::before {
  content: "";
  display: inline-block;
  width: 4px;
  height: 16px;
  background: #409EFF;
  margin-right: 10px;
  border-radius: 2px;
}

.preview-content {
  line-height: 1.8;
  color: #5e6d82;
  font-size: 15px;
}

/* 深度选择器 */
.preview-content >>> img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin: 8px 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.preview-content >>> p {
  margin-bottom: 1.2em;
  color: #3c4858;
}

.preview-content >>> h1,
.preview-content >>> h2,
.preview-content >>> h3 {
  color: #1f2d3d;
  margin: 20px 0 15px;
}

.preview-content >>> a {
  color: #409EFF;
  text-decoration: none;
  transition: color 0.2s;
}

.preview-content >>> a:hover {
  color: #337ecc;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .rich-text-editor-container {
    padding: 24px 16px;
    margin: 10px;
  }

  .action-buttons {
    flex-direction: column;
    gap: 12px;
  }

  .action-btn {
    width: 100%;
  }
}
</style>
